<?php


namespace app\admin\controller\packagec;

use app\common\controller\Adminbase;

use think\Db;

class Package2 extends Adminbase
{
//    protected $searchFields = 'id,username,nickname';

    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        $package_id = $this->request->param('package_id/d', 0);
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[]=['pr.is_del','=',0];
            $where[]=['pr.package_id','=',$package_id];
            $where[]=['pr.type','=',2];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['pr.id', '=', $filter['id']];
                }

                if (isset($filter['salesperson_id'])) {
                    $where[] = ['s.id', '=', $filter['salesperson_id']];
                }

                if (isset($filter['sale_name'])) {
                    $where[] = ['s.name', 'like', '%' . $filter['sale_name'] . '%'];
                }
            }
            $_list = Db::name('package_reward')->alias('pr')
                ->join('package p','p.id=pr.package_id')
                ->join('salesperson s','s.id=pr.salesperson_id')
                ->field('pr.*,s.name as sale_name,p.name as package_name')
                ->where($where)
                ->page($page, $limit)
                ->order('pr.create_time desc')
                ->select();
            $total = Db::name('package_reward')->alias('pr')
                ->join('package p','p.id=pr.package_id')
                ->join('salesperson s','s.id=pr.salesperson_id')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        foreach ($area_list as $k => $v) {
            $proviec[$v['name']] = $v['name'];
        }
        $this->assign([
            'proviec'=>$proviec,
            'package_id'=>$package_id
        ]);
        return $this->fetch();
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');


        if (Db::name('package_reward')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //删除
    public function del()
    {

        $ids = $this->request->param('ids/a', null);

        try {
            foreach ($ids as $id) {
                Db::name('package_reward')->where('id', $id)->update([
                    'is_del'=>1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    //增加
    public function add()
    {
        $package_id = $this->request->param('package_id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();

            //获取套餐初始奖励总金额
            $sum_commission=Db::name('package')->where('id','=',$data['package_id'])->value('commission1 + commission2');
            if (!$sum_commission){
                $sum_commission=0;
            }
            //服务费总金额
            $reward=Db::name('package')->where('id','=',$data['package_id'])->value('reward1 + reward2');
            if (!$reward){
                $reward=0;
            }
            if ($data['row']['reward2']>$reward){
                $this->error('奖励金额不能大于套餐总额度');
            }
            if ($data['row']['commission2']>$sum_commission){
                $this->error('服务费不能大于套餐总额度');
            }

            $package_reward_info=Db::name('package_reward')->where([
                ['package_id','=',$data['package_id']],
                ['salesperson_id','=',$data['row']['salesperson_id']],
                ['type','=',2],
                ['is_del','=',0]
            ])->find();
            if ($package_reward_info){
                $this->error('已设置此员工！');
            }
            $shop=Db::name('salesperson')->where('id','=',$data['row']['salesperson_id'])->find();
            if (!$shop){
                $this->error('员工不存在！');
            }
            $data['row']['package_id']=$data['package_id'];
            $data['row']['create_time']=time();
            $data['row']['type']=2;
            $stmt = Db::name('package_reward')->insertGetId($data['row']);

            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $proviec_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        $this->assign([
            'proviec' => $proviec_list,
            'package_id'=>$package_id
        ]);
        return $this->fetch();

    }

    public function edit()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->Post();

            //获取套餐初始奖励总金额
            $sum_commission=Db::name('package')->where('id','=',$data['package_id'])->value('commission1 + commission2');
            if (!$sum_commission){
                $sum_commission=0;
            }
            //服务费总金额
            $reward=Db::name('package')->where('id','=',$data['package_id'])->value('reward1 + reward2');
            if (!$reward){
                $reward=0;
            }
            if ($data['row']['reward2']>$reward){
                $this->error('奖励金额不能大于套餐总额度');
            }
            if ($data['row']['commission2']>$sum_commission){
                $this->error('服务费不能大于套餐总额度');
            }

            $stmt = Db::name('package_reward')
                ->where(['id' => $data['id']])
                ->update($data['row']);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }
        $proviec_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();

        $data = Db::name('package_reward')->find($id);
        $this->assign([
            'data'=>$data,
            'proviec' => $proviec_list
        ]);
        return $this->fetch();
    }

}